#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>



void executa()
{
    if(fork() == 0){
        close(1);
        dup(varpipe[1]);
        close(varpipe[0]);
        execlp("ls", "ls", "/", NULL);
    }
    else if(fork() == 0){
        dup2(varpipe[0], 0);
        close(varpipe[1]);
        execlp("grep", "grep", "-i", "e", NULL);
    }
    wait(NULL);
}



int main()
{
    pipe(varpipe);

    char c;
    printf("Executa comandos entre 'pipe'. digite 'x' para sair.\n");

    while(scanf("%c", &c) && c != 'x'){
        executa();
    }

    return 0;
}
